<?php
	autoLoader();
	
	$objPatient 	= new Patients();
	$objCasePaper 	= new CasePapers();
	$objChargeTypes = new ChargeTypes();
	
	if( true == isset( $_GET['patient_id'] ) ) {
		$objPatient = $objPatient->fetchPatientsById( $_GET['patient_id'] );
	}
	
	if( true == isset( $_GET['case_paper_id'] ) && true == valStr( $_GET['case_paper_id'] ) ) {
		$objCasePaper = $objCasePaper->fetchCasePapersById( $_GET['case_paper_id'] );
	}

	if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
		$objUrine 	= new Urines();
		$strSql 	= 'SELECT * FROM urines WHERE case_paper_id = ' . $objCasePaper->getId() . ';';
		
		$arrobjUrines = $objUrine->fetchSql( $strSql );
	}
	
	$strSql = 'SELECT * FROM charge_types WHERE charge_catagory_id = ' . ChargeCatagoriesType::CHARGE_CATAGORY_LAB . ';';
	$arrobjChargeTypes = $objChargeTypes->fetchSql( $strSql );
	
	if( true == isset( $_GET['urine_id'] ) && true == valStr( $_GET['urine_id'] ) ) {
		$objUrine = new Urines();
		$objLoadedUrine = $objUrine->fetchUrinesById( $_GET['urine_id'] );
	}
	
	if( true == valObj( $objLoadedUrine, 'BaseUrines' ) ) {
		$objCharge = new Charges();
		$objCharge = $objCharge->fetchChargesById( $objLoadedUrine->getChargeId() );
	}
	
?>
<div id="loaded_urine">
<fieldset style="border:1px solid gray; width:100%;">
<legend style="padding:0.2em 0.5em;border:1px solid gray;color:#2C6193;font-size:13px;text-align:left;font-weight:bold;margin-bottom: 1px;">Urine Report</legend>
	
	<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-success" id="urine_success_display">
		<img alt="urine_insert_success Found with info.." src="images/ok.png" width="20" height="20">
		<label id='urine_insert_success'></label>
	</div>
	
	<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-danger" id="urine_failed_display">
		<img alt="Patient Found with info.." src="images/close.png" width="20" height="20">
		<label id='urine_insert_failed'></label>
	</div>
	
	<div style="clear: both;"></div>
	
	<div id="left_side_continer" style="float: left; border-style: solid; border-color: #2C6193; width: 22%; height: 100px; background-color: #CCCCFF;">
		<?php
			if( true == valArr( $arrobjUrines ) ) {
				echo '<ul id="list-nav">';
				foreach( $arrobjUrines as $objUrine ) {
					echo '<a onclick="loadUrineData(' . $objUrine->getId() . ')"><li>' . $objUrine->getId() . '->' . $objUrine->getColor() . '</li></a>';
				}
				echo '</ul>';
			} else {
				echo "<label>No Urine Report Avaliable</label>";
			}
		?>
	</div>
	
	<div style="float: right; width: 76%;">
		<form action="" method="post" enctype="multipart/form-data" id="urine_insert_form">
			<div>
				<div style="width: 50%; float: left;">
					Charges<label style="color: red;">*</label>
					<select name="charge_applied" id="charge_applied" onchange="calculateAmount();">
						<option value="">Select Charge</option>
						<?php
							if( true == valArr( $arrobjChargeTypes ) ) {
								foreach( $arrobjChargeTypes as $objChargeType ) {
									$intChargeAmount 	= ( true == valObj( $objPatient, 'BasePatients' ) && PatientType::PATIENT_CB == $objPatient->getPatientTypeId() ) ? $objChargeType->getCbArea() : $objChargeType->getOutOfCa();
									$strSelected 		= ( ( true == valObj( $objLoadedUrine, 'BaseUrines' ) && true == valObj( $objCharge, 'BaseCharges' ) && $objCharge->getChargeTypeId() == $objChargeType->getId() ) ? 'selected="selected"' : NULL );
									echo '<option id="' . $intChargeAmount . '" value="' . $objChargeType->getId() . '"' . $strSelected . '>' . $objChargeType->getName() . '</option>';
								}
							}
						?>
					</select>
				</div>
				<div><?php $intChargeAmount = ( ( true == valObj( $objCharge, 'BaseCharges' ) ) ? $objCharge->getRate() : 0 ); ?> Charge Applied: <label id="selectd_charge_amount"><?php echo $intChargeAmount; ?></label></div>
				<div style="clear: both;"></div>
			</div>
			<div style="clear: both;"></div>
			
			<div>
			<?php
				$strColor = $strDeposit = $strReaction = $strAlbumin = $strSugar = $strScetene = $strDiaceticAcid = $strBileSacts = $strBileDigments = $strPusCells = $strPBC = $strCasts = $strCrystals = $strCells = $strMicroOrganisms = $strRemark = $strChargeId = NULL;
				
					if( true == valObj( $objLoadedUrine, 'BaseUrines' ) ) {
						$strColor 			= $objLoadedUrine->getColor();
						$strDeposit 		= $objLoadedUrine->getDeposit();
						$strReaction 		= $objLoadedUrine->getReaction();
						$strAlbumin 		= $objLoadedUrine->getAlbumin();
						$strSugar 			= $objLoadedUrine->getSugar();
						$strScetene 		= $objLoadedUrine->getScetene();
						$strDiaceticAcid 	= $objLoadedUrine->getDiaceticAcid();
						$strBileSacts 		= $objLoadedUrine->getBileSacts();
						$strBileDigments 	= $objLoadedUrine->getBileDigments();
						$strPusCells 		= $objLoadedUrine->getPusCells();
						$strPBC 			= $objLoadedUrine->getPbc();
						$strCasts 			= $objLoadedUrine->getCasts();
						$strCrystals 		= $objLoadedUrine->getCrystals();
						$strCells 			= $objLoadedUrine->getCells();
						$strMicroOrganisms 	= $objLoadedUrine->getMicroOrganisms();
						
						$intChargeId 		= $objLoadedUrine->getChargeId();
	
						$strRemark 			= $objLoadedUrine->getRemarks();
					}
			?>
				<table style="border-style: solid;" class="table table-striped table-bordered table-hover dataTable no-footer">
					<tr>
						<th style="width: 30%;">Particular Name</th>
						<th style="width: 30%;">Test Result</th>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Color</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strColor; ?>" name="color" id="color" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Deposit</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strDeposit; ?>" name="deposit" id="deposit" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Reaction</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strReaction; ?>" name="reaction" id="reaction" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Albumin</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strAlbumin; ?>" name="albumin" id="albumin" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Sugar</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strSugar; ?>" name="sugar" id="sugar" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Scetene</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strScetene; ?>" name="scetene" id="scetene" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Diacetic Acid</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strDiaceticAcid; ?>" name="diacetic_acid" id="diacetic_acid" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Bile Sacts</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strBileSacts; ?>" name="bile_sacts" id="bile_sacts" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Bile Digments</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strBileDigments; ?>" name="bile_digments" id="bile_digments" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Pus Cells</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strPusCells; ?>" name="pus_cells" id="pus_cells" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">P.B.C</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strPBC; ?>" name="p_b_c" id="p_b_c" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Casts</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strCasts; ?>" name="casts" id="casts" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Crystals</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strCrystals; ?>" name="crystals" id="crystals" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Cells</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strCells; ?>" name="cells" id="cells" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px; width: 30%;">Micro Organisms</td>
						<td style="line-height: 1; padding: 2px; width: 30%;"><input type="text" value="<?php echo $strMicroOrganisms; ?>" name="micro_organisms" id="micro_organisms" /></td>
					</tr>
				</table>
			</div>
			
			<div>
				<div style="float: left;">Remark (optional)</div>
				<div style="margin: 5px; padding-left: 10px; float: left;">
					<textarea rows="3" cols="20" name="remark"><?php echo $strRemark; ?></textarea>
				</div>
				<div style="clear: both;"></div>
			</div>
			
			<div style="margin-top: 15px;">
				<div>
					<?php
						if( true == isset( $_GET['urine_id'] ) && true == valStr( $_GET['urine_id'] ) ) {
							echo "<input type=hidden name=operation value=update />";
							echo "<input type=button value=UPDATE id=submit name=submit onclick='insertUrineForm();' />";
							echo "<input type=button value=Add New id=add_new name=add_new onclick='addNewXRayReport();' />";
							echo "<input type=button value=Cancel id=cancel name=cancel />";
						} else {
							echo "<input type=hidden name=operation value=insert />";
							echo "<input type=button value=SUBMIT id=Submit name=submit onclick='insertUrineForm();' />";
							echo "<input type=button value=Cancel id=cancel name=cancel onclick='closeXray();' />";
						}
					?>
				</div>
				<?php if( true == valStr( $intChargeId ) ) {?><input type="hidden" name="charge_id" value="<?php echo $intChargeId; ?>" /><?php } ?>
				<input type="hidden" name="urine_id" value="<?php echo ( true == isset( $_GET['urine_id'] ) ) ? $_GET['urine_id'] : NULL; ?>" />
				<input type="hidden" name="patient_id" value="<?php echo ( true == isset( $_GET['patient_id'] ) ) ? $_GET['patient_id'] : NULL; ?>" />
				<input type="hidden" name="case_paper_id" value="<?php echo ( true == isset( $_GET['case_paper_id'] ) ) ? $_GET['case_paper_id'] : NULL; ?>" />
			</div>
		</form>
	</div>
</fieldset>
</div>
<script>
	function insertUrineForm() {
		var intPatientId =	$('#patient_details').val();
		var postData = $('#urine_insert_form').serializeArray();
	
		strUrl = "modules.php?file=insert_urine_form&type=insert";
	
		$.ajax({
			url: strUrl,
			type: "post",
			data: postData,
			success: function( returnData ) {
				strCustomerData = returnData;
				if( 1 == strCustomerData ) {
					$('#urine_success_display').css( 'display', 'block' );

					$('#urine_failed_display').css( 'display', 'none' );

					loadAllUrineData();
				} else {
					$('#urine_failed_display').css( 'display', 'block' );
					$('#urine_insert_failed').html( 'Failed to insert Urine details' );

					$('#urine_success_display').css( 'display', 'none' );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function loadUrineData( intUrineId ) {
		var intPatientId 	=	$('#patient_id').val();
		var intCasePaperId 	=	$('#case_paper_id').val();
		
		var strUrl = "modules.php?file=urine_test&type=forms&urine_id=" + intUrineId + '&patient_id=' + intPatientId + '&case_paper_id=' + intCasePaperId;

		$.ajax({
			url: strUrl,
			type: "post",
			data: 'urine_id=' + intUrineId,
			success: function( returnData ) {
				strUrineData = returnData;
				
				if( strUrineData != '' ) {
					$('#loaded_urine').html( strUrineData );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function addNewXRayReport() {
		var intPatientId 	=	$('#patient_id').val();
		var intCasePaperId 	=	$('#case_paper_id').val();
		
		var strUrl = 'modules.php?file=urine_test&type=forms&patient_id=' + intPatientId + '&case_paper_id=' + intCasePaperId;

		$.ajax({
			url: strUrl,
			type: "post",
			data: '',
			success: function( returnData ) {
				strXrayData = returnData;
				
				if( strXrayData != '' ) {
					$('#loaded_urine').html( strXrayData );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function calculateAmount() {
		var intChargeAmount = $( "#charge_applied option:selected" ).attr( 'id' );
		$('#selectd_charge_amount' ).html( intChargeAmount );
	}

	function loadAllUrineData() {
		var intPatientId 	=	$('#patient_id').val();
		var intCasePaperId 	=	$('#case_paper_id').val();

		$strUrl = "modules.php?file=urine_test&type=forms&patient_id=" + intPatientId + "&case_paper_id=" + intCasePaperId;
	
		$.ajax({
			url: $strUrl,
			type: "post",
			data: "patient_data=" + intPatientId,
			beforeSend: function() {
				$("#wait").css("display","block");
				$('.col-lg-10').css("display","none");
			},
			success: function( returnData ) {
				strCustomerData = returnData;
				$('#urine_from' ). html( strCustomerData );
				$( "#urine_from" ).show( 300 );

				$('#urine_insert_success').html( 'Urine details inserted Or updated successfully.' );
				$('#urine_success_display').css( 'display', 'block' );
				$('#urine_failed_display').css( 'display', 'none' );

				$("#wait").css("display","none");
				$('.col-lg-10').css("display","block");
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function closeXray() {
		$('#urine_test').attr( 'checked', false );
		$('#urine_from' ). html( '' );
	}
</script>
<style>
ul#list-nav {
	list-style:none;
	margin-top:5px;
	margin-left:5px;
	padding:0;
	width: 94%;
}

ul#list-nav li {
	//display:inline;
	background-color:#F19102;
	color:#6A6B8F;
	font-size: 15px;
	margin-bottom: 10px;
	cursor:pointer;
}

ul#list-nav li a {
	text-decoration: none;
	padding: 5px 0;
	width: 100px;
	color: #eee;
	//float: left;
	text-align: center;
	border-bottom: 1px solid #fff;
}

ul#list-nav li:hover {
	background:#a2b3a1;
	color:#000;
}

td.min_height {
	line-height: 1;
	padding: 2px;
}

.pc_data {
	display: none;
}

textarea {
	resize: vertical;
}
</style>